﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using JTicket.Common;
using System.Web.Security;
using System.Security.Principal;
namespace JTicket.UI
{
    public partial class Default : System.Web.UI.Page
    {
        private readonly BLL.C_CodeNote bllCodeNote = new BLL.C_CodeNote();
        private readonly BLL.D_OperInfo bllOper = new BLL.D_OperInfo();
        protected void Page_Load(object sender, EventArgs e)
        {

            string CodeNote2 = DAL.C_CodeNote.GetIsUse();
            if (Convert.ToDateTime(CodeNote2) <= DateTime.Now)
            {
                Response.Write("系统已过期(" + CodeNote2 + "),请联系开发商。。。。");
                Response.End();
            }
            string CodeNote = DAL.C_CodeNote.GetSystemState();
            if (CodeNote != "1")
            {
                Response.Write("系统维护中。。。。");
                Response.End();
            }
            if (!IsPostBack)
            {
                this.Page.Title = bllCodeNote.GetModel("system", "002").CodeNote;
                this.cop.Text = bllCodeNote.GetModel("system", "001").CodeNote;
            }
            if (Request["Flag"] != null)
                if (Request["Flag"].CompareTo("Login") != -1)
                    Check_Login(Request["username"], Request["password"], Request["code1"]);
        }

        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="code"></param>
        protected void Check_Login(string username, string password, string code)
        {
            int ii = 0;
            string cc = string.Empty;
            //比对验证码
            if (Request.Cookies["checkcode"]["code"] != null)
            {
                cc = Server.HtmlEncode(Request.Cookies["checkcode"]["code"]);
                if (code.ToLower().Equals(cc.ToLower()))
                    ii = 1;
                else
                    JsMsg.Alert("验证码错误！");
            }
            //验证码验证成功
            if (ii == 1)
            {
                if (BLL.D_OperInfo.OperLogin(username, password))
                {
                    Model.D_OperInfo model = bllOper.GetModel(username);
                    //保存用户信息.
                    Session["OperInfo"] = model;
                    //记录日志
                    Model.L_Log ModelLog = new Model.L_Log();
                    DateTime dt = DateTime.Now;
                    ModelLog.Flag = "1";
                    ModelLog.LogDate = dt.ToShortDateString();
                    ModelLog.LogTime = dt.ToShortTimeString();
                    ModelLog.LogTxt = "登录系统成功";
                    ModelLog.OperCode = username;
                    ModelLog.OperName = model.OperName;
                    ModelLog.ThCode = model.ThName;
                    ModelLog.LogIP = Request.UserHostAddress;
                    DAL.L_Log.Add(ModelLog);


                    //验证授权
                    HttpCookie httpcookie = new HttpCookie("SaleType", model.SaleType);
                    Response.Cookies.Add(httpcookie);
                    FormsAuthentication.SetAuthCookie(username, false);
                    JsMsg.OpenWebForm("JT_Main.aspx", true, false);
                    JsMsg.WriteJavaScript("self.close();");
                }
                else
                {
                    //记录日志
                    Model.L_Log ModelLog = new Model.L_Log();
                    DateTime dt = DateTime.Now;
                    ModelLog.Flag = "1";
                    ModelLog.LogDate = dt.ToShortDateString();
                    ModelLog.LogTime = dt.ToShortTimeString();
                    ModelLog.LogTxt = "登录系统失败,返回【用户名或密码错误！】";
                    ModelLog.OperCode = username;
                    ModelLog.OperName = "未知";
                    ModelLog.ThCode = "未知";
                    ModelLog.LogIP = Request.UserHostAddress;
                    DAL.L_Log.Add(ModelLog);
                    JsMsg.Alert("用户名或密码错误！");

                }

            }
        }
    }
}